﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Reflection;
using System.Text;

namespace Common
{
    public class DTEnums
    {
        /// <summary>
        /// 将枚举转为集合
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static List<EnumEntity> EnumToList<T>()
        {
            List<EnumEntity> list = new List<EnumEntity>();

            foreach (var e in Enum.GetValues(typeof(T)))
            {
                EnumEntity m = new EnumEntity();
                object[] objArr = e.GetType().GetField(e.ToString()).GetCustomAttributes(typeof(DescriptionAttribute), true);
                if (objArr != null && objArr.Length > 0)
                {
                    DescriptionAttribute da = objArr[0] as DescriptionAttribute;
                    m.Describe = da.Description;
                }
                m.PrameKey = Convert.ToInt32(e);
                m.PrameValue = e.ToString();
                list.Add(m);
            }
            return list;
        }

        /// <summary>
        /// 获取枚举描述
        /// </summary>
        /// <param name="enumValue">枚举值</param>
        /// <returns></returns>
        public string GetEnumDescription(Enum enumValue)
        {
            string value = enumValue.ToString();
            FieldInfo field = enumValue.GetType().GetField(value);
            object[] objs = field.GetCustomAttributes(typeof(DescriptionAttribute), false); //获取描述属性
            if (objs == null || objs.Length == 0) //当描述属性没有时，直接返回名称
                return value;
            DescriptionAttribute descriptionAttribute = (DescriptionAttribute)objs[0];
            return descriptionAttribute.Description;
        }
        /// <summary>
        /// 由枚举值得到枚举描述
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="value"></param>g.
        /// <returns></returns>
        public static string GetEnumDescription<T>(string value)
        {
            Type type = typeof(T);
            Dictionary<string, string> dic = EnumFieldDescription(type);
            string description = string.Empty;
            if (!string.IsNullOrWhiteSpace(value))
            {
                if (dic.ContainsKey(Convert.ToString((T)Enum.Parse(type, value))))
                {
                    description = dic[Convert.ToString((T)Enum.Parse(type, value))];
                }
            }
            return description;
        }
        /// <summary>
        /// 得到enum字段描述列表
        /// </summary>
        /// <returns>返回字典</returns>
        public static Dictionary<string, string> EnumFieldDescription(Type enumType)
        {
            Dictionary<string, string> dic = new Dictionary<string, string>();
            Type type = typeof(DescriptionAttribute);
            FieldInfo[] fields = enumType.GetFields();
            foreach (FieldInfo field in fields)
            {
                object[] arr = field.GetCustomAttributes(type, true);
                if (arr.Length > 0)
                {
                    dic.Add(field.Name, ((DescriptionAttribute)arr[0]).Description);
                }
            }
            return dic;
        }
        /// <summary>
        /// 统一管理操作枚举
        /// </summary>
        public enum ActionEnum
        {
            /// <summary>
            /// 所有
            /// </summary>
            All,
            /// <summary>
            /// 显示
            /// </summary>
            Show,
            /// <summary>
            /// 查看
            /// </summary>
            View,
            /// <summary>
            /// 添加
            /// </summary>
            Add,
            /// <summary>
            /// 修改
            /// </summary>
            Edit,
            /// <summary>
            /// 删除
            /// </summary>
            Delete,
            /// <summary>
            /// 审核
            /// </summary>
            Audit,
            /// <summary>
            /// 确认
            /// </summary>
            Confirm,
            /// <summary>
            /// 取消
            /// </summary>
            Cancel,
            /// <summary>
            /// 登录
            /// </summary>
            Login
        }
        /// <summary>
        /// 订单状态枚举说明
        /// </summary>
        public enum EnumOrderState
        {
            /// <summary>
            /// 废单
            /// </summary>
            [Description("废单")]
            AbandonedOrder = -1,
            /// <summary>
            /// 0新建订单
            /// </summary>
            [Description("新建订单")]
            NewCreateOrder = 0,
            /// <summary>
            /// 审核不通过
            /// </summary>
            [Description("审核不通过")]
            AuditFail = 1,
            /// <summary>
            /// 审核通过
            /// </summary>
            [Description("审核通过")]
            AuditSuccess = 2,
            /// <summary>
            /// 申请退款
            /// </summary>
            [Description("申请退款")]
            ApplyReback = 3,
            /// <summary>
            /// 退款成功
            /// </summary>
            [Description("退款成功")]
            ReBackMoney = 4,
            /// <summary>
            /// 已完成
            /// </summary>
            [Description("已完成")]
            FinishOrder = 5
        }

        /// <summary>
        /// 资源性别枚举
        /// </summary>
        public enum EnumPersonSex
        {
            [Description("男")]
            Male = 1,
            [Description("女")]
            FaMale = 0
        }


        /// <summary>
        /// 商品规格单位
        /// </summary>
        public enum EnumSpecUnit
        {
            [Description("件")]
            Jian = 0,
            [Description("箱")]
            Xiang = 1
        }

    }
    /// <summary>
    /// 通用枚举模板类
    /// </summary>
    public class EnumEntity
    {
        /// <summary>
        /// 属性键
        /// </summary>
        public int PrameKey { set; get; }
        /// <summary>
        /// 属性之
        /// </summary>
        public string PrameValue { set; get; }
        /// <summary>
        /// 描述说明
        /// </summary>
        public string Describe { set; get; }
    }
}
